#ifndef _LOG_H_
#include "head.h"

#define DEBUG 1
#ifdef DEBUG

//  LOGD--deg信息  LOGI--Info信息  LOGE--Error信息 
//  LOGI(...) LOG(INFO, __VA_ARGS__)--> ... -- __VA_ARGS__ ;例子： LOGI("输出：% d\n", a); __VA_ARGS__ -- "输出：% d\n"
#define LOG1(Severity, format, ...)\
{\
    time_t now = time(NULL);                                   \
    struct tm *tm_info = localtime(&now);                       \
    char time_str[26];                                        \
    strftime(time_str, 26, "%Y-%m-%d %H:%M:%S", tm_info);     \
    int temp=dup(STDOUT_FILENO);\
    int fd = open("log.info", O_RDWR|O_APPEND);\
    ERROR_CHECK(fd, -1, "open");\
    dup2(fd, STDOUT_FILENO);\
    printf("[%s]%s/%s/line%d %s:\t" format "\n", #Severity, __FILE__, __FUNCTION__, __LINE__, time_str, ##__VA_ARGS__);\
    fflush(stdout);\
    dup2(temp, STDOUT_FILENO);\
    close(fd);\
}

// LOGE--Error信息
#define LOG2(Severity, format, ...)\
{\
    time_t now = time(NULL);                                   \
    struct tm *tm_info = localtime(&now);                       \
    char time_str[26];                                        \
    strftime(time_str, 26, "%Y-%m-%d %H:%M:%S", tm_info);     \
    int temp=dup(STDOUT_FILENO);\
    int fd = open("log.error", O_RDWR|O_APPEND);\
    ERROR_CHECK(fd, -1, "open");\
    dup2(fd, STDOUT_FILENO);\
    printf("[%s]%s/%s/line%d %s:\t" format "\n", #Severity, __FILE__, __FUNCTION__, __LINE__, time_str, ##__VA_ARGS__);\
    fflush(stdout);\
    dup2(temp, STDOUT_FILENO);\
    close(fd);\
}


#define LOGD(...) LOG(DEBUG, __VA_ARGS__)
#define LOGI(...) LOG1(INFO, __VA_ARGS__)
#define LOGW(...) LOG(WARRING, __VA_ARGS__)
#define LOGE(...) LOG2(ERROR, __VA_ARGS__)
#define LOGF(...) LOG(FALT, __VA_ARGS__) abort();
#else
#define LOG(Severity, format, ...) 
#define LOGD(...) 
#define LOGI(...) 
#define LOGW(...) 
#define LOGE(...) 
#define LOGF(...) 
#endif

#endif
